<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
    >
      <el-form-item label="名称" prop="cropName">
        <el-input
            v-model="queryParams.cropName"
            placeholder="请输入名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
        >搜索
        </el-button>
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >新增
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table :data="cropList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" label="序号" width="80"/>
      <el-table-column label="名称" align="center" prop="cropName"/>
      <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer
        v-model="drawer"
        :title="title"
        :append-to-body="true"
        :modal-append-to-body="true"
        destroy-on-close="true"
        size="50%"
    >
      <el-form label-width="80px" :model="form" :rules="rules" ref="form">
        <el-form-item label="名称">
          <el-input
              v-model="form.cropName"
              placeholder="请输入品种名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">立即创建</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {addCrop, cropAllAdmin, deleteCrop,updateCrop,updateCropAdmin} from "@/api/crop";
import {ElMessageBox} from "element-plus";

export default {
  name: "PlantCrop",

  data() {
    return {
      // 遮罩层
      loading: true,
      //
      title: "新增",
      drawer: false,
      adOpen: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // sad表格数据
      cropList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        cropName: ""
      },
      // 表单参数
      form: {
        cropName: ""
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询sad列表 */
    getList() {
      console.log("=======================,", this.drawer);
      // this.loading = true;
      cropAllAdmin(this.queryParams).then(response => {
        console.log("response", response);
        console.log("=======================,", response.data);
        this.cropList = response.data;
        console.log("=======================,", this.cropList);
        this.total = response.total;
        //   this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.drawer = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cropName: null
      };
      // this.$refs['form'].resetFields();
      // debugger
      this.$refs.form.resetFields();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      (this.form = {
        plant: {
          cropName: ""
        },
        plantBreed: {},
        plantEcotope: {}
      }),
          (this.drawer = true);
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.$router.push("/admin/plant/AlterDetails")

      // console.log("=======================,", this.drawer);
      const id = row.cropId;
      updateCrop(id).then(response => {
        this.drawer = true;
        this.form = response.data;
        console.log("=======================,", this.form);
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.cropId==null){
      //   addCrop(this.form).then(response => {
      //   if (response.success) {
      //     this.$message({
      //       message: "新增种类成功!",
      //       type: "success",
      //       showClose: true
      //     });
      //     this.open = false;
      //     this.getList();
      //   } else {
      //     this.$message({
      //       message: "新增种类失败!",
      //       type: "error",
      //       showClose: true
      //     });
      //   }
      // });
      addCrop(this.form).then(response=>{

      })
      }else{
        updateCropAdmin
        updateCropAdmin(this.form).then(response=>{

})
      }
     
      // } else {
      //     addPlant(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //     });
      // }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log("=======================,", row);
      const id = row.cropId;
      ElMessageBox.confirm("是否确认删除数据项?", "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(function () {
            return deleteCrop(id);
          })
          .then(() => {
            ElMessageBox.confirm("删除成功");
          });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
          "system/plant/export",
          {
            ...this.queryParams
          },
          `plant_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
<style lang="scss" scoped>
.demo-image__error .image-slot {
  font-size: 30px;
}

.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}

.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}
</style>
